package main.string;

import com.alibaba.fastjson.JSONObject;
import main.string.utill.CertificateTemplateDTO;
import main.string.utill.ItemLimitRuleVO;
import main.string.utill.JsonUtil;
import org.junit.Test;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @create 2017-03-17 21:52
 **/
public class ObjectToJson {

	@Test
	public void test2() throws Exception {
		List<ItemLimitRuleVO> itemLimitRuleVOS = new ArrayList<ItemLimitRuleVO>();
		ItemLimitRuleVO itemLimitRuleVO = new ItemLimitRuleVO();
		itemLimitRuleVO.setSkuId(111L);
		itemLimitRuleVO.setAgentGrade(2L);
		itemLimitRuleVO.setPrice("3000");
		itemLimitRuleVO.setOrderMin("60");
		itemLimitRuleVO.setDayMax("1000");
		itemLimitRuleVO.setWeekMax("100000");
		itemLimitRuleVO.setMonthMax("10000000");
		itemLimitRuleVO.setRemark("remark1");

		ItemLimitRuleVO itemLimitRuleVO2 = new ItemLimitRuleVO();
		itemLimitRuleVO2.setSkuId(111L);
		itemLimitRuleVO2.setAgentGrade(3L);
		itemLimitRuleVO2.setPrice("3500");
		itemLimitRuleVO2.setOrderMin("50");
		itemLimitRuleVO2.setDayMax("1000");
		itemLimitRuleVO2.setWeekMax("100000");
		itemLimitRuleVO2.setMonthMax("10000000");
		itemLimitRuleVO2.setRemark("remark1");

		itemLimitRuleVOS.add(itemLimitRuleVO);
		itemLimitRuleVOS.add(itemLimitRuleVO2);

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("content", itemLimitRuleVOS);
		System.out.println("json:{}" + jsonObject.toString());

		String content = "[{\"agent_grade\":2,\"day_max\":\"1000\",\"month_max\":\"10000000\",\"order_min\":\"60\",\"price\":\"3000\",\"remark\":\"remark1\",\"sku_id\":111,\"week_max\":\"100000\"},{\"agent_grade\":3,\"day_max\":\"1000\",\"month_max\":\"10000000\",\"order_min\":\"50\",\"price\":\"3500\",\"remark\":\"remark1\",\"sku_id\":112,\"week_max\":\"100000\"}]";

		Type type = new com.google.gson.reflect.TypeToken<List<ItemLimitRuleVO>>() {
		}.getType();
		List<ItemLimitRuleVO> agentLimitRuleVO = JsonUtil.parseJson(content,
				type);
		System.out.println(
				"agentLimitRuleVO...:{}" + JsonUtil.toJson(agentLimitRuleVO));
	}

	@Test
	public void test3() throws Exception {
		CertificateTemplateDTO certificate= new CertificateTemplateDTO();
		certificate.setName("一叶子");
		certificate.setBackPicture("www.baidu.com/1.jpg");
		certificate.setIsCertificate(0);

		List<CertificateTemplateDTO> result = new ArrayList<CertificateTemplateDTO>();

		CertificateTemplateDTO certificateTemplateDTO = new CertificateTemplateDTO();
		certificateTemplateDTO.setWidth("width");
		certificateTemplateDTO.setHeight("height");
		certificateTemplateDTO.setFontColor("fontColor");
		certificateTemplateDTO.setFontSize("fontSize");
		certificateTemplateDTO.setTypeFace("typeFace");
		certificateTemplateDTO.setIsImage(0);
		certificateTemplateDTO.setIsBold(1);
		certificateTemplateDTO.setX("20");
		certificateTemplateDTO.setY("30");
		certificateTemplateDTO.setAppX("40");
		certificateTemplateDTO.setAppY("50");
		certificateTemplateDTO.setIsCertificate(1);
		result.add(certificateTemplateDTO);

		CertificateTemplateDTO certificateTemplateDTO2 = new CertificateTemplateDTO();
		certificateTemplateDTO2.setWidth("width");
		certificateTemplateDTO2.setHeight("height");
		certificateTemplateDTO2.setFontColor("fontColor");
		certificateTemplateDTO2.setFontSize("fontSize");
		certificateTemplateDTO2.setTypeFace("typeFace");
		certificateTemplateDTO2.setIsImage(0);
		certificateTemplateDTO2.setIsBold(1);
		certificateTemplateDTO2.setX("20");
		certificateTemplateDTO2.setY("30");
		certificateTemplateDTO2.setAppX("40");
		certificateTemplateDTO2.setAppY("50");
		certificateTemplateDTO2.setIsCertificate(1);
		result.add(certificateTemplateDTO2);

		JSONObject jsonObject =new JSONObject();
		jsonObject.put("id",2L);
		jsonObject.put("name","一叶子");
		jsonObject.put("list",result);
		jsonObject.put("is_Certificate",0);

		System.out.println("json:{}"+jsonObject.toString());

		String content = "{\"id\":2,\"list\":[{\"app_x\":\"40\",\"app_y\":\"50\",\"font_color\":\"font_color\",\"font_size\":\"font_size\",\"height\":\"height\",\"is_bold\":1,\"is_image\":0,\"typez_face\":\"typeFace\",\"width\":\"width\",\"x\":\"20\",\"y\":\"30\"},{\"appX\":\"40\",\"appY\":\"50\",\"font_color\":\"fontColor\",\"font_size\":\"fontSize\",\"height\":\"height\",\"is_bold\":1,\"is_image\":0,\"type_face\":\"typeFace\",\"width\":\"width\",\"x\":\"20\",\"y\":\"30\"}],\"name\":\"一叶子\"}";

		Type type = new com.google.gson.reflect.TypeToken<CertificateTemplateDTO>() {
		}.getType();
		CertificateTemplateDTO agentLimitRuleVO = JsonUtil.parseJson(content,type);
		System.out.println(
				"agentLimitRuleVO...:{}" + JsonUtil.toJson(agentLimitRuleVO));
	}

	@Test public void test4() throws Exception {
		List<Long> list = new ArrayList<Long>();
		list.add(2L);
		list.add(3L);
		list.add(4L);
		System.out.println(JsonUtil.toJson(list));

		String content = "[2,3,4]";
		Type type = new com.google.gson.reflect.TypeToken<List<Long>>() {
		}.getType();
		List<Long> agentLimitRuleVO = JsonUtil.parseJson(content,type);
		System.out.println(
				"agentLimitRuleVO...:{}" + JsonUtil.toJson(agentLimitRuleVO));
	}

}
